package com.cty.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.cty.kafka.common.bo.User;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * HelloController
 *
 * @author tianyuchan
 * @since 2024/7/13
 */

@RestController
public class HelloController {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("/hello")
    public String hello(){
        kafkaTemplate.send("hello-topic", "Hello Kafka!");
        return "ok";
    }

    @GetMapping("/user")
    public String user(){
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        kafkaTemplate.send("user-topic", JSON.toJSONString(user));
        return "ok";
    }
}
